package org.ghost.spring.cloud.feign.demo.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.ghost.spring.cloud.feign.demo.dto.RspDTO;
import org.ghost.spring.cloud.feign.demo.dto.weather.WeatherDTO;
import org.ghost.spring.cloud.feign.demo.service.IHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping(value = "demo")
public class DemoController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private IHelloService helloService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EurekaClient eurekaClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public RspDTO get(@PathVariable String id) {
        return this.render(helloService.get(id));
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public RspDTO post(@RequestBody WeatherDTO reqDTO) {
        return this.render(helloService.post(reqDTO));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public RspDTO put(@PathVariable String id, @RequestBody WeatherDTO reqDTO) {
        return this.render(helloService.put(id, reqDTO));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public RspDTO delete(@PathVariable String id) {
        return this.render(helloService.delete(id));
    }

    @RequestMapping(value = "test1", method = RequestMethod.GET)
    public RspDTO get1() throws ExecutionException, InterruptedException {
        return this.render(this.helloService.test1());
    }

    @RequestMapping(value = "test2", method = RequestMethod.GET)
    public RspDTO get2() throws ExecutionException, InterruptedException {
        String url = "http://PROVIDER/demo/test2";
        return restTemplate.getForObject(url, RspDTO.class);
    }

    @RequestMapping(value = "test3", method = RequestMethod.GET)
    public RspDTO get3() {
        return this.helloService.test3();
    }

    @RequestMapping(value = "test4", method = RequestMethod.GET)
    public RspDTO get4() {
        return this.helloService.test4();
    }

    @RequestMapping(value = "test5", method = RequestMethod.GET)
    public RspDTO get5() {
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("PROVIDER", false);
        return this.render(instanceInfo);
    }

    @RequestMapping(value = "test6", method = RequestMethod.GET)
    public RspDTO get6() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("PROVIDER");
        return this.render(serviceInstance.getHost() + ":" + serviceInstance.getPort());
    }


    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

